#include "editwidget.h"

//TODO Put everything here in scheme structures

EditWidget::EditWidget( QMap<QString, QStringList> albums_attrs)
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
readSettings();

opened_album_attrs = albums_attrs;
current_record = QSqlRecord();
current_index = QModelIndex();
is_new = false;

/*
QDate curr_date = QDate::currentDate();
dateLabel->setText( curr_date.toString("d MMMM yyyy") );
*/

QLineEdit * nomLE = qobject_cast<QLineEdit *>(nominalComboBox->lineEdit());
nomLE->setMaxLength(42);
nominalComboBox->addItems( opened_album_attrs.value("denominations") );
nominalComboBox->setCurrentIndex(-1);

QLineEdit * marksLE = qobject_cast<QLineEdit *>(marksComboBox->lineEdit());
marksLE->setMaxLength(42);
marksComboBox->addItems( opened_album_attrs.value("marks") );
marksComboBox->setCurrentIndex(-1);

QLineEdit * stateLE = qobject_cast<QLineEdit *>(stateComboBox->lineEdit());
stateLE->setMaxLength(42);
stateComboBox->addItems( opened_album_attrs.value("states") );
stateComboBox->setCurrentIndex(-1);

QLineEdit * epochLE = qobject_cast<QLineEdit *>(epochComboBox->lineEdit());
epochLE->setMaxLength(42);
epochComboBox->addItems( opened_album_attrs.value("epochs") );
epochComboBox->setCurrentIndex(-1);

QLineEdit * edgeIDLE = qobject_cast<QLineEdit *>(edgeIDComboBox->lineEdit());
edgeIDLE->setMaxLength(21);
edgeIDComboBox->addItems( opened_album_attrs.value("edgeids") );
edgeIDComboBox->setCurrentIndex(-1);

QLineEdit * materialLE = qobject_cast<QLineEdit *>(materialComboBox->lineEdit());
materialLE->setMaxLength(42);
materialComboBox->addItems( opened_album_attrs.value("materials") );
materialComboBox->setCurrentIndex(-1);

QLineEdit * typeLE = qobject_cast<QLineEdit *>(typeComboBox->lineEdit());
typeLE->setMaxLength(42);
typeComboBox->addItems( opened_album_attrs.value("types") );
typeComboBox->setCurrentIndex(-1);

QLineEdit * undertypeLE = qobject_cast<QLineEdit *>(undertypeComboBox->lineEdit());
undertypeLE->setMaxLength(42);
undertypeComboBox->addItems( opened_album_attrs.value("undertypes") );
undertypeComboBox->setCurrentIndex(-1);

QLineEdit * mintLE = qobject_cast<QLineEdit *>(mintComboBox->lineEdit());
mintLE->setMaxLength(42);
mintComboBox->addItems( opened_album_attrs.value("mints") );
mintComboBox->setCurrentIndex(-1);

QLineEdit * obvsymLE = qobject_cast<QLineEdit *>(obvSymbolComboBox->lineEdit());
obvsymLE->setMaxLength(42);
obvSymbolComboBox->addItems( opened_album_attrs.value("obvsymbs") );
obvSymbolComboBox->setCurrentIndex(-1);

QLineEdit * revsymLE = qobject_cast<QLineEdit *>(revSymbolComboBox->lineEdit());
revsymLE->setMaxLength(42);
revSymbolComboBox->addItems( opened_album_attrs.value("revsymbs") );
revSymbolComboBox->setCurrentIndex(-1);

QLineEdit * obvtypeLE = qobject_cast<QLineEdit *>(obvTypeComboBox->lineEdit());
obvtypeLE->setMaxLength(42);
obvTypeComboBox->addItems( opened_album_attrs.value("obvtypes") );
obvTypeComboBox->setCurrentIndex(-1);

QLineEdit * revtypeLE = qobject_cast<QLineEdit *>(revTypeComboBox->lineEdit());
revtypeLE->setMaxLength(42);
revTypeComboBox->addItems( opened_album_attrs.value("revtypes") );
revTypeComboBox->setCurrentIndex(-1);

QLineEdit * priceLE = qobject_cast<QLineEdit *>(priceCurrComboBox->lineEdit());
priceLE->setMaxLength(10);
priceCurrComboBox->addItems( opened_album_attrs.value("currencies") );
priceCurrComboBox->setCurrentIndex(-1);

QLineEdit * estimLE = qobject_cast<QLineEdit *>(estPriceCurrComboBox->lineEdit());
estimLE->setMaxLength(10);
estPriceCurrComboBox->addItems( opened_album_attrs.value("currencies") );
estPriceCurrComboBox->setCurrentIndex(-1);

statusComboBox->addItems( opened_album_attrs.value("statuses") );

QLineEdit * condLE = qobject_cast<QLineEdit *>(condComboBox->lineEdit());
condLE->setMaxLength(10);
condComboBox->addItems( opened_album_attrs.value("conditions") );
condComboBox->setCurrentIndex(-1);

QLineEdit * certauthLE = qobject_cast<QLineEdit *>(certAuthComboBox->lineEdit());
certauthLE->setMaxLength(10);
certAuthComboBox->addItems( opened_album_attrs.value("cert_auths") );
certAuthComboBox->setCurrentIndex(-1);

QLineEdit * weightLE = qobject_cast<QLineEdit *>(weightUnitComboBox->lineEdit());
weightLE->setMaxLength(10);
weightUnitComboBox->addItems( opened_album_attrs.value("units") );
weightUnitComboBox->setCurrentIndex(-1);

QStringList fu;
QString tr_round = tr("round");
QString tr_quad = tr("quadrant");
QString tr_tri = tr("triangle");
fu << tr_round << tr_quad << tr_tri;
QLineEdit * shapeLE = qobject_cast<QLineEdit *>(shapeComboBox->lineEdit());
shapeLE->setMaxLength(21);
shapeComboBox->addItems(fu);
shapeComboBox->setCurrentIndex(-1);

QLineEdit * diamUnitLE = qobject_cast<QLineEdit *>(diamUnitComboBox->lineEdit());
diamUnitLE->setMaxLength(10);
diamUnitComboBox->addItems( opened_album_attrs.value("units") );
diamUnitComboBox->setCurrentIndex(-1);

QLineEdit * thickUnitLE = qobject_cast<QLineEdit *>(thickUnitComboBox->lineEdit());
thickUnitLE->setMaxLength(10);
thickUnitComboBox->addItems( opened_album_attrs.value("units") );
thickUnitComboBox->setCurrentIndex(-1);

QLineEdit * axis1LE = qobject_cast<QLineEdit *>(axis1ComboBox->lineEdit());
axis1LE->setMaxLength(21);
QString tr_ax1 = tr("coin");
QString tr_ax2 = tr("medall");
QStringList ax;
ax << tr_ax1 << tr_ax2;
axis1ComboBox->addItems(ax);
axis1ComboBox->setCurrentIndex(-1);

QLineEdit * locLE = qobject_cast<QLineEdit *>(locComboBox->lineEdit());
locLE->setMaxLength(21);
locComboBox->addItems( opened_album_attrs.value("locations") );
locComboBox->setCurrentIndex(-1);

QLineEdit * custLE = qobject_cast<QLineEdit *>(custComboBox->lineEdit());
custLE->setMaxLength(42);
//custComboBox->addItems();

QLineEdit * sellerLE = qobject_cast<QLineEdit *>(sellerComboBox->lineEdit());
sellerLE->setMaxLength(42);
//sellerComboBox->addItems();

QLineEdit * recTypeLE = qobject_cast<QLineEdit *>(recTypeComboBox->lineEdit());
recTypeLE->setMaxLength(21);
recTypeComboBox->addItems( opened_album_attrs.value("rectypes") );
recTypeComboBox->setCurrentIndex(-1);

QObject::connect( saveToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( save_clicked() ) );
QObject::connect( charsToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( show_chars() ) );
QObject::connect( cancelToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( close() ) );

}

EditWidget::~EditWidget()
{
}

// TODO
// put in schema structures
void EditWidget::set_record(QSqlRecord record,
                            QModelIndex index,
                            QString opened_album)
{
    if ( true == opened_album.isEmpty() )
    {
    return;
    }

    if (!record.isNull("DENOMINATION") )
    {
    QString nominal = record.value("DENOMINATION").toString();
    nominalCheckBox->setChecked(true);
    nominalComboBox->setEditText(nominal);
    }
    else
    {
    nominalCheckBox->setChecked(false);
    nominalComboBox->clearEditText();
    }

    if (!record.isNull("OBJ_YEAR") )
    {
    yearCheckBox->setChecked(true);
    yearSpinBox->setValue( record.value("OBJ_YEAR").toInt() );
    }
    else
    {
    yearCheckBox->setChecked(false);
    yearSpinBox->clear();
    }

    if (!record.isNull("MARKS") )
    {
    QString marks = record.value("MARKS").toString();
    marksCheckBox->setChecked(true);
    marksComboBox->setEditText(marks);
    }
    else
    {
    marksCheckBox->setChecked(false);
    marksComboBox->clearEditText();
    }

    if (!record.isNull("RATING") )
    {
    rarCheckBox->setChecked(true);
    rarSpinBox->setValue( record.value("RATING").toInt() );
    }
    else
    {
    rarCheckBox->setChecked(false);
    }

    if (!record.isNull("STATE") )
    {
    QString state = record.value("STATE").toString();
    stateCheckBox->setChecked(true);
    stateComboBox->setEditText(state);
    }
    else
    {
    stateCheckBox->setChecked(false);
    stateComboBox->clearEditText();
    }

    if (!record.isNull("EPOCH") )
    {
    QString epoch = record.value("EPOCH").toString();
    epochCheckBox->setChecked(true);
    epochComboBox->setEditText(epoch);
    }
    else
    {
    epochCheckBox->setChecked(false);
    epochComboBox->clearEditText();
    }

    if (!record.isNull("EDGE") )
    {
    QString edge = record.value("EDGE").toString();
    edgeCheckBox->setChecked(true);
    edgeLineEdit->setText(edge);
    }
    else
    {
    edgeCheckBox->setChecked(false);
    edgeLineEdit->clear();
    }

    if (!record.isNull("MATERIAL") )
    {
    QString material = record.value("MATERIAL").toString();
    materialCheckBox->setChecked(true);
    materialComboBox->setEditText(material);
    }
    else
    {
    materialCheckBox->setChecked(false);
    materialComboBox->clearEditText();
    }

    if (!record.isNull("GRADED") )
    {
    QString graded = record.value("GRADED").toString();
        if (graded == "Y")
        {
        gradedEditCheckBox->setChecked(true);
        }
    }
    else
    {
    gradedEditCheckBox->setChecked(false);
    }

    if (!record.isNull("CONDITION") )
    {
    QString condition = record.value("CONDITION").toString();
    conditionCheckBox->setChecked(true);
    condComboBox->setEditText(condition);
    }
    else
    {
    conditionCheckBox->setChecked(false);
    condComboBox->clearEditText();
    }

    if (!record.isNull("CERTIFIED") )
    {
    QString certified = record.value("CERTIFIED").toString();
        if (certified == "Y")
        {
        certEditCheckBox->setChecked(true);
        }
    }
    else
    {
    certEditCheckBox->setChecked(false);
    }

    if (!record.isNull("CERT_AUTH") )
    {
    QString cert_auth = record.value("CERT_AUTH").toString();
    certAuthEditCheckBox->setChecked(true);
    certAuthComboBox->setEditText(cert_auth);
    }
    else
    {
    certAuthEditCheckBox->setChecked(false);
    certAuthComboBox->clearEditText();
    }

    if (!record.isNull("COND_NOTE") )
    {
    QString cond_note = record.value("COND_NOTE").toString();
    condNoteCheckBox->setChecked(true);
    condNoteLineEdit->setText(cond_note);
    }
    else
    {
    condNoteCheckBox->setChecked(false);
    }

    if (!record.isNull("WEIGHT") )
    {
    weightCheckBox->setChecked(true);
    weightDoubleSpinBox->setValue( record.value("WEIGHT").toDouble() );

        if (!record.isNull("WEIGHT_UNIT") )
        {
        QString weight_unit = record.value("WEIGHT_UNIT").toString();
        weightUnitComboBox->setEditText(weight_unit);
        }
        else
        {
        weightUnitComboBox->clearEditText();
        }
    }
    else
    {
    weightCheckBox->setChecked(false);
    }

    if (!record.isNull("DIAM") )
    {
    diamCheckBox->setChecked(true);
    diamDoubleSpinBox->setValue( record.value("DIAM").toDouble() );

        if (!record.isNull("DIAM_UNIT") )
        {
        QString diam_unit = record.value("DIAM_UNIT").toString();
        diamUnitComboBox->setEditText(diam_unit);
        }
        else
        {
        diamUnitComboBox->clearEditText();
        }

    }
    else
    {
    diamCheckBox->setChecked(false);
    }

    if (!record.isNull("AXIS1") )
    {
    QString axis1 = record.value("AXIS1").toString();
    axis1CheckBox->setChecked(true);
    axis1ComboBox->setEditText(axis1);
    }
    else
    {
    axis1CheckBox->setChecked(false);
    axis1ComboBox->clearEditText();
    }

    if (!record.isNull("AXIS2") )
    {
    QString axis2 = record.value("AXIS2").toString();
    axis2CheckBox->setChecked(true);
    axis2LineEdit->setText(axis2);
    }
    else
    {
    axis2CheckBox->setChecked(false);
    axis2LineEdit->clear();
    }

    if (!record.isNull("SHAPE") )
    {
    QString shape = record.value("SHAPE").toString();
    shapeCheckBox->setChecked(true);
    shapeComboBox->setEditText(shape);
    }
    else
    {
    shapeCheckBox->setChecked(false);
    shapeComboBox->clearEditText();
    }

    if (!record.isNull("THICKNESS") )
    {
    thickCheckBox->setChecked(true);
    thickDoubleSpinBox->setValue( record.value("THICKNESS").toDouble() );

        if (!record.isNull("THICKNESS_UNIT") )
        {
        QString thick_unit = record.value("THICKNESS_UNIT").toString();
        thickUnitComboBox->setEditText(thick_unit);
        }
        else
        {
        thickUnitComboBox->clearEditText();
        }

    }
    else
    {
    thickCheckBox->setChecked(false);
    }

    if (!record.isNull("EDGEID") )
    {
    QString edgeID = record.value("EDGEID").toString();
    edgeIDCheckBox->setChecked(true);
    edgeIDComboBox->setEditText(edgeID);
    }
    else
    {
    edgeIDCheckBox->setChecked(false);
    edgeIDComboBox->clearEditText();
    }

    if (!record.isNull("OBJ_TYPE") )
    {
    QString obj_type = record.value("OBJ_TYPE").toString();
    typeCheckBox->setChecked(true);
    typeComboBox->setEditText(obj_type);
    }
    else
    {
    typeCheckBox->setChecked(false);
    typeComboBox->clearEditText();
    }

    if (!record.isNull("OBJ_UNDERTYPE") )
    {
    QString obj_undertype = record.value("OBJ_UNDERTYPE").toString();
    undertypeCheckBox->setChecked(true);
    undertypeComboBox->setEditText(obj_undertype);
    }
    else
    {
    undertypeCheckBox->setChecked(false);
    undertypeComboBox->clearEditText();
    }

    if (!record.isNull("EDGE_LEGEND") )
    {
    QString edge_legend = record.value("EDGE_LEGEND").toString();
    edgeLegendCheckBox->setChecked(true);
    edgeLegendLineEdit->setText(edge_legend);
    }
    else
    {
    edgeLegendCheckBox->setChecked(false);
    edgeLegendLineEdit->clear();
    }

    if (!record.isNull("OBVERSEARTIST") )
    {
    QString obverseartist = record.value("OBVERSEARTIST").toString();
    artistObvCheckBox->setChecked(true);
    artistObvLineEdit->setText(obverseartist);
    }
    else
    {
    artistObvCheckBox->setChecked(false);
    artistObvLineEdit->clear();
    }

    if (!record.isNull("REVERSEARTIST") )
    {
    QString reverseartist = record.value("REVERSEARTIST").toString();
    artistRevCheckBox->setChecked(true);
    artistRevLineEdit->setText(reverseartist);
    }
    else
    {
    artistRevCheckBox->setChecked(false);
    artistRevLineEdit->clear();
    }

    if (!record.isNull("OBVERSEENGRAVER") )
    {
    QString obverseengraver = record.value("OBVERSEENGRAVER").toString();
    engraverObvCheckBox->setChecked(true);
    engraverObvLineEdit->setText(obverseengraver);
    }
    else
    {
    engraverObvCheckBox->setChecked(false);
    engraverObvLineEdit->clear();
    }

    if (!record.isNull("REVERSEENGRAVER") )
    {
    QString reverseengraver = record.value("REVERSEENGRAVER").toString();
    engraverRevCheckBox->setChecked(true);
    engraverRevLineEdit->setText(reverseengraver);
    }
    else
    {
    engraverRevCheckBox->setChecked(false);
    engraverRevLineEdit->clear();
    }

    if (!record.isNull("ISSUER") )
    {
    QString issuer = record.value("ISSUER").toString();
    issuerCheckBox->setChecked(true);
    issuerLineEdit->setText(issuer);
    }
    else
    {
    issuerCheckBox->setChecked(false);
    issuerLineEdit->clear();
    }

    if (!record.isNull("AUTHORITY") )
    {
    QString auth = record.value("AUTHORITY").toString();
    authCheckBox->setChecked(true);
    authLineEdit->setText(auth);
    }
    else
    {
    authCheckBox->setChecked(false);
    authLineEdit->clear();
    }

    if (!record.isNull("MINT") )
    {
    QString mint = record.value("MINT").toString();
    mintCheckBox->setChecked(true);
    mintComboBox->setEditText(mint);
    }
    else
    {
    mintCheckBox->setChecked(false);
    mintComboBox->clearEditText();
    }

    if (!record.isNull("MINTAGE_NOTE") )
    {
    QString mintage_note = record.value("MINTAGE_NOTE").toString();
    mintageNoteCheckBox->setChecked(true);
    mintageNoteLineEdit->setText(mintage_note);
    }
    else
    {
    mintageNoteCheckBox->setChecked(false);
    mintageNoteLineEdit->clear();
    }

    if (!record.isNull("MINTAGE") )
    {
    int mintage = record.value("MINTAGE").toInt();
    mintageCheckBox->setChecked(true);
    mintageSpinBox->setValue(mintage);
    }
    else
    {
    mintageCheckBox->setChecked(false);
    mintageSpinBox->clear();
    }

    if (!record.isNull("OBVERSESYMBOL") )
    {
    QString obversesymbol = record.value("OBVERSESYMBOL").toString();
    obvSymbolCheckBox->setChecked(true);
    obvSymbolComboBox->setEditText(obversesymbol);
    }
    else
    {
    obvSymbolCheckBox->setChecked(false);
    obvSymbolComboBox->clearEditText();
    }

    if (!record.isNull("OBVERSETYPE") )
    {
    QString obversetype = record.value("OBVERSETYPE").toString();
    obvTypeCheckBox->setChecked(true);
    obvTypeComboBox->setEditText(obversetype);
    }
    else
    {
    obvTypeCheckBox->setChecked(false);
    obvTypeComboBox->clearEditText();
    }

    if (!record.isNull("OBVERSELEGEND1") )
    {
    QString obverselegend1 = record.value("OBVERSELEGEND1").toString();
    obvLegend1CheckBox->setChecked(true);
    obvLegend1LineEdit->setText(obverselegend1);
    }
    else
    {
    obvLegend1CheckBox->setChecked(false);
    obvLegend1LineEdit->clear();
    }

    if (!record.isNull("OBVERSELEGEND2") )
    {
    QString obverselegend2 = record.value("OBVERSELEGEND2").toString();
    obvLegend2CheckBox->setChecked(true);
    obvLegend2LineEdit->setText(obverselegend2);
    }
    else
    {
    obvLegend2CheckBox->setChecked(false);
    obvLegend2LineEdit->clear();
    }

    if (!record.isNull("OBVERSEDIEID") )
    {
    QString obversedieid = record.value("OBVERSEDIEID").toString();
    obvDieIDCheckBox->setChecked(true);
    obvDieIDComboBox->setEditText(obversedieid);
    }
    else
    {
    obvDieIDCheckBox->setChecked(false);
    obvDieIDComboBox->clear();
    }

    if (!record.isNull("REVERSEDIEID") )
    {
    QString reversedieid = record.value("REVERSEDIEID").toString();
    revDieIDCheckBox->setChecked(true);
    revDieIDComboBox->setEditText(reversedieid);
    }
    else
    {
    revDieIDCheckBox->setChecked(false);
    revDieIDComboBox->clear();
    }

    if (!record.isNull("OBVERSECOUNTERMARK") )
    {
    QString obversecountermark = record.value("OBVERSECOUNTERMARK").toString();
    obvMarkCheckBox->setChecked(true);
    obvMarkLineEdit->setText(obversecountermark);
    }
    else
    {
    obvMarkCheckBox->setChecked(false);
    obvMarkLineEdit->clear();
    }

    if (!record.isNull("REVERSESYMBOL") )
    {
    QString reversesymbol = record.value("REVERSESYMBOL").toString();
    revSymbolCheckBox->setChecked(true);
    revSymbolComboBox->setEditText(reversesymbol);
    }
    else
    {
    revSymbolCheckBox->setChecked(false);
    revSymbolComboBox->clearEditText();
    }

    if (!record.isNull("REVERSETYPE") )
    {
    QString reversetype = record.value("REVERSETYPE").toString();
    revTypeCheckBox->setChecked(true);
    revTypeComboBox->setEditText(reversetype);
    }
    else
    {
    revTypeCheckBox->setChecked(false);
    revTypeComboBox->clearEditText();
    }

    if (!record.isNull("REVERSELEGEND1") )
    {
    QString reverselegend1 = record.value("REVERSELEGEND1").toString();
    revLegend1CheckBox->setChecked(true);
    revLegend1LineEdit->setText(reverselegend1);
    }
    else
    {
    revLegend1CheckBox->setChecked(false);
    revLegend1LineEdit->clear();
    }

    if (!record.isNull("REVERSELEGEND2") )
    {
    QString reverselegend2 = record.value("REVERSELEGEND2").toString();
    revLegend2CheckBox->setChecked(true);
    revLegend2LineEdit->setText(reverselegend2);
    }
    else
    {
    revLegend2CheckBox->setChecked(false);
    revLegend2LineEdit->clear();
    }

    if (!record.isNull("REVERSECOUNTERMARK") )
    {
    QString reversecountermark = record.value("REVERSECOUNTERMARK").toString();
    revMarkCheckBox->setChecked(true);
    revMarkLineEdit->setText(reversecountermark);
    }
    else
    {
    revMarkCheckBox->setChecked(false);
    revMarkLineEdit->clear();
    }

    if (!record.isNull("FROMDATE") )
    {
    QString fromdate = record.value("FROMDATE").toString();
    fromCheckBox->setChecked(true);
    fromLineEdit->setText(fromdate);
    }
    else
    {
    fromCheckBox->setChecked(false);
    fromLineEdit->clear();
    }

    if (!record.isNull("TODATE") )
    {
    QString todate = record.value("TODATE").toString();
    toCheckBox->setChecked(true);
    toLineEdit->setText(todate);
    }
    else
    {
    toCheckBox->setChecked(false);
    toLineEdit->clear();
    }

    if (!record.isNull("DATEONOBJECT") )
    {
    QString dateonobject = record.value("DATEONOBJECT").toString();
    dateObjCheckBox->setChecked(true);
    dateObjLineEdit->setText(dateonobject);
    }
    else
    {
    dateObjCheckBox->setChecked(false);
    dateObjLineEdit->clear();
    }

    if (!record.isNull("ERAONOBJECT") )
    {
    QString eraonobject = record.value("ERAONOBJECT").toString();
    eraObjCheckBox->setChecked(true);
    eraObjLineEdit->setText(eraonobject);
    }
    else
    {
    eraObjCheckBox->setChecked(false);
    eraObjLineEdit->clear();
    }

    if (!record.isNull("RULERONOBJECT") )
    {
    QString ruleronobject = record.value("RULERONOBJECT").toString();
    rulerObjCheckBox->setChecked(true);
    rulerObjLineEdit->setText(ruleronobject);
    }
    else
    {
    rulerObjCheckBox->setChecked(false);
    rulerObjLineEdit->clear();
    }

    if (!record.isNull("REGION") )
    {
    QString region = record.value("REGION").toString();
    regionCheckBox->setChecked(true);
    regionLineEdit->setText(region);
    }
    else
    {
    regionCheckBox->setChecked(false);
    regionLineEdit->clear();
    }

    if (!record.isNull("FINDSPOT") )
    {
    QString findspot = record.value("FINDSPOT").toString();
    fsCheckBox->setChecked(true);
    fsLineEdit->setText(findspot);
    }
    else
    {
    fsCheckBox->setChecked(false);
    fsLineEdit->clear();
    }

    if (!record.isNull("COORDINATES") )
    {
    QString coordinates = record.value("COORDINATES").toString();
    coordCheckBox->setChecked(true);
    coordLineEdit->setText(coordinates);
    }
    else
    {
    coordCheckBox->setChecked(false);
    coordLineEdit->clear();
    }

    if (!record.isNull("LATITUDE") )
    {
    latitudeCheckBox->setChecked(true);
    latitudeSpinBox->setValue( record.value("LATITUDE").toDouble() );
    }
    else
    {
    latitudeCheckBox->setChecked(false);
    }

    if (!record.isNull("LONGITUDE") )
    {
    longitudeCheckBox->setChecked(true);
    longitudeSpinBox->setValue( record.value("LONGITUDE").toDouble() );
    }
    else
    {
    longitudeCheckBox->setChecked(false);
    }

    if (!record.isNull("STATUS") )
    {
    QString status = record.value("STATUS").toString();
    statusCheckBox->setChecked(true);
    statusComboBox->setEditText(status);
    }
    else
    {
    statusCheckBox->setChecked(false);
    statusComboBox->clearEditText();
    }

    if (true != record.isNull("AQ_DATE") )
    {
    QString aq_date = record.value("AQ_DATE").toString();
    aqDateCheckBox->setChecked(true);
    aqDateLineEdit->setText(aq_date);
    }
    else
    {
    aqDateCheckBox->setChecked(false);
    QDate d = QDate::currentDate();
    }

    if (!record.isNull("LOCATION") )
    {
    QString loc = record.value("LOCATION").toString();
    locCheckBox->setChecked(true);
    locComboBox->setEditText(loc);
    }
    else
    {
    locCheckBox->setChecked(false);
    locComboBox->clearEditText();
    }

    if (!record.isNull("SELLER") )
    {
    QString seller = record.value("SELLER").toString();
    sellerCheckBox->setChecked(true);
    sellerComboBox->setEditText(seller);
    }
    else
    {
    sellerCheckBox->setChecked(false);
    sellerComboBox->clearEditText();
    }

    if (!record.isNull("AQ_PRICE") )
    {
    priceCheckBox->setChecked(true);
    priceDoubleSpinBox->setValue( record.value("AQ_PRICE").toDouble() );
        if (!record.isNull("AQ_CURR") )
        {
        QString price_curr = record.value("AQ_CURR").toString();
        priceCurrComboBox->setEditText(price_curr);
        }
        else
        {
        priceCurrComboBox->clearEditText();
        }
    }
    else
    {
    priceCheckBox->setChecked(false);
    }

    if (!record.isNull("ACT_VALUE") )
    {
    estPriceCheckBox->setChecked(true);
    estPriceDoubleSpinBox->setValue( record.value("ACT_VALUE").toDouble() );
        if (!record.isNull("ACT_CURR") )
        {
        QString estim_curr = record.value("ACT_CURR").toString();
        estPriceCurrComboBox->setEditText(estim_curr);
        }
        else
        {
        estPriceCurrComboBox->clearEditText();
        }
    }
    else
    {
    estPriceCheckBox->setChecked(false);
    }

    if (!record.isNull("SOLD_DATE") )
    {
    int date_int = record.value("SOLD_DATE").toInt();
    QString date_str = QString::number(date_int);
    QDate date = QDate::fromString( date_str, "yyyyMMdd" );

        if ( true == date.isValid() )
        {
        soldDateCheckBox->setChecked(true);
        dateEdit->setDate(date);
        }
        else
        {
        soldDateCheckBox->setChecked(false);
        dateEdit->setDate( QDate::currentDate() );
        }

    }
    else
    {
    soldDateCheckBox->setChecked(false);
    dateEdit->setDate( QDate::currentDate() );
    }

    if (!record.isNull("CUSTOMER") )
    {
    QString cust = record.value("CUSTOMER").toString();
    custCheckBox->setChecked(true);
    custComboBox->setEditText(cust);
    }
    else
    {
    custCheckBox->setChecked(false);
    custComboBox->clearEditText();
    }

    if (!record.isNull("TITLE") )
    {
    QString titel = record.value("TITLE").toString();
    titelCheckBox->setChecked(true);
    titelLineEdit->setText(titel);
    }
    else
    {
    titelCheckBox->setChecked(false);
    titelLineEdit->clear();
    }

    if (!record.isNull("UNIQUEID") )
    {
    QString uniqueid = record.value("UNIQUEID").toString();
    uniqIDCheckBox->setChecked(true);
    uniqIDLineEdit->setText(uniqueid);
    }
    else
    {
    uniqIDCheckBox->setChecked(false);
    uniqIDLineEdit->clear();
    }

    if (!record.isNull("COLLECTION") )
    {
    QString collection = record.value("COLLECTION").toString();
    collCheckBox->setChecked(true);
    collLineEdit->setText(collection);
    }
    else
    {
    collCheckBox->setChecked(false);
    collLineEdit->clear();
    }

    if (!record.isNull("COLLECTIONID") )
    {
    QString collectionID = record.value("COLLECTIONID").toString();
    collIDCheckBox->setChecked(true);
    collIDLineEdit->setText(collectionID);
    }
    else
    {
    collIDCheckBox->setChecked(false);
    collIDLineEdit->clear();
    }

    if (!record.isNull("PROJECTID") )
    {
    QString projectID = record.value("PROJECTID").toString();
    projIDCheckBox->setChecked(true);
    projIDLineEdit->setText(projectID);
    }
    else
    {
    projIDCheckBox->setChecked(false);
    projIDLineEdit->clear();
    }

    if (!record.isNull("HOARD") )
    {
    QString hoard = record.value("HOARD").toString();
    hoardCheckBox->setChecked(true);
    hoardLineEdit->setText(hoard);
    }
    else
    {
    hoardCheckBox->setChecked(false);
    hoardLineEdit->clear();
    }

    if (!record.isNull("CATALOGID") )
    {
    QString catalogid = record.value("CATALOGID").toString();
    catIDCheckBox->setChecked(true);
    catIDLineEdit->setText(catalogid);
    }
    else
    {
    catIDCheckBox->setChecked(false);
    catIDLineEdit->clear();
    }

    if (!record.isNull("HISTORY_NOTE") )
    {
    QString history_note = record.value("HISTORY_NOTE").toString();
    historyCheckBox->setChecked(true);
    historyLineEdit->setText(history_note);
    }
    else
    {
    historyCheckBox->setChecked(false);
    historyLineEdit->clear();
    }

    if (!record.isNull("RECORDDATE") )
    {
    QString recorddate = record.value("RECORDDATE").toString();
    recCreatedCheckBox->setChecked(true);
    recCreatedLineEdit->setText(recorddate);
    }
    else
    {
    recCreatedCheckBox->setChecked(false);
    recCreatedLineEdit->clear();
    }

    if (!record.isNull("RECORDTYPE") )
    {
    QString recordtype = record.value("RECORDTYPE").toString();
    recTypeCheckBox->setChecked(true);
    recTypeComboBox->setEditText(recordtype);
    }
    else
    {
    recTypeCheckBox->setChecked(false);
    recTypeComboBox->clearEditText();
    }

    if (!record.isNull("RESERVED_1") )
    {
    QString reserved_1 = record.value("RESERVED_1").toString();
    res1CheckBox->setChecked(true);
    res1LineEdit->setText(reserved_1);
    }
    else
    {
    res1CheckBox->setChecked(false);
    res1LineEdit->clear();
    }

    if (!record.isNull("RESERVED_2") )
    {
    QString reserved_2 = record.value("RESERVED_2").toString();
    res2CheckBox->setChecked(true);
    res2LineEdit->setText(reserved_2);
    }
    else
    {
    res2CheckBox->setChecked(false);
    res2LineEdit->clear();
    }

    if (!record.isNull("RESERVED_3") )
    {
    QString reserved_3 = record.value("RESERVED_3").toString();
    res3CheckBox->setChecked(true);
    res3LineEdit->setText(reserved_3);
    }
    else
    {
    res3CheckBox->setChecked(false);
    res3LineEdit->clear();
    }

    if (!record.isNull("RESERVED_4") )
    {
    QString reserved_4 = record.value("RESERVED_4").toString();
    res4CheckBox->setChecked(true);
    res4LineEdit->setText(reserved_4);
    }
    else
    {
    res4CheckBox->setChecked(false);
    res4LineEdit->clear();
    }

    if (!record.isNull("RESERVED_5") )
    {
    QString reserved_5 = record.value("RESERVED_5").toString();
    res5CheckBox->setChecked(true);
    res5LineEdit->setText(reserved_5);
    }
    else
    {
    res5CheckBox->setChecked(false);
    res5LineEdit->clear();
    }

    if (!record.isNull("RESERVED_6") )
    {
    QString reserved_6 = record.value("RESERVED_6").toString();
    res6CheckBox->setChecked(true);
    res6LineEdit->setText(reserved_6);
    }
    else
    {
    res6CheckBox->setChecked(false);
    res6LineEdit->clear();
    }

    if (!record.isNull("RESERVED_7") )
    {
    QString reserved_7 = record.value("RESERVED_7").toString();
    res7CheckBox->setChecked(true);
    res7LineEdit->setText(reserved_7);
    }
    else
    {
    res7CheckBox->setChecked(false);
    res7LineEdit->clear();
    }

    if (!record.isNull("RESERVED_8") )
    {
    QString reserved_8 = record.value("RESERVED_8").toString();
    res8CheckBox->setChecked(true);
    res8LineEdit->setText(reserved_8);
    }
    else
    {
    res8CheckBox->setChecked(false);
    res8LineEdit->clear();
    }

    if (!record.isNull("RESERVED_9") )
    {
    QString reserved_9 = record.value("RESERVED_9").toString();
    res9CheckBox->setChecked(true);
    res9LineEdit->setText(reserved_9);
    }
    else
    {
    res9CheckBox->setChecked(false);
    res9LineEdit->clear();
    }

    if (!record.isNull("RESERVED_10") )
    {
    QString reserved_10 = record.value("RESERVED_10").toString();
    res10CheckBox->setChecked(true);
    res10LineEdit->setText(reserved_10);
    }
    else
    {
    res10CheckBox->setChecked(false);
    res10LineEdit->clear();
    }

    if (!record.isNull("RESERVED_11") )
    {
    QString reserved_11 = record.value("RESERVED_11").toString();
    res11CheckBox->setChecked(true);
    res11LineEdit->setText(reserved_11);
    }
    else
    {
    res11CheckBox->setChecked(false);
    res11LineEdit->clear();
    }

    if (!record.isNull("RESERVED_12") )
    {
    QString reserved_12 = record.value("RESERVED_12").toString();
    res12CheckBox->setChecked(true);
    res12LineEdit->setText(reserved_12);
    }
    else
    {
    res12CheckBox->setChecked(false);
    res12LineEdit->clear();
    }

    if (!record.isNull("RESERVED_13") )
    {
    int reserved_13 = record.value("RESERVED_13").toInt();
    res13CheckBox->setChecked(true);
    res13SpinBox->setValue(reserved_13);
    }
    else
    {
    res13CheckBox->setChecked(false);
    res13SpinBox->clear();
    }

    if (!record.isNull("RESERVED_14") )
    {
    int reserved_14 = record.value("RESERVED_14").toInt();
    res14CheckBox->setChecked(true);
    res14SpinBox->setValue(reserved_14);
    }
    else
    {
    res14CheckBox->setChecked(false);
    res14SpinBox->clear();
    }

    if (!record.isNull("RESERVED_15") )
    {
    int reserved_15 = record.value("RESERVED_15").toInt();
    res15CheckBox->setChecked(true);
    res15SpinBox->setValue(reserved_15);
    }
    else
    {
    res15CheckBox->setChecked(false);
    res15SpinBox->clear();
    }

    if (!record.isNull("RESERVED_16") )
    {
    int reserved_16 = record.value("RESERVED_16").toInt();
    res16CheckBox->setChecked(true);
    res16SpinBox->setValue(reserved_16);
    }
    else
    {
    res16CheckBox->setChecked(false);
    res16SpinBox->clear();
    }

    if (!record.isNull("RESERVED_17") )
    {
    double reserved_17 = record.value("RESERVED_17").toDouble();
    res17CheckBox->setChecked(true);
    res17DoubleSpinBox->setValue(reserved_17);
    }
    else
    {
    res17CheckBox->setChecked(false);
    res17DoubleSpinBox->clear();
    }

    if (!record.isNull("RESERVED_18") )
    {
    double reserved_18 = record.value("RESERVED_18").toDouble();
    res18CheckBox->setChecked(true);
    res18DoubleSpinBox->setValue(reserved_18);
    }
    else
    {
    res18CheckBox->setChecked(false);
    res18DoubleSpinBox->clear();
    }

    if (!record.isNull("RESERVED_19") )
    {
    qint64 reserved_19 = record.value("RESERVED_19").toLongLong();
    res19CheckBox->setChecked(true);
    res19LineEdit->setText( QString::number(reserved_19) );
    }
    else
    {
    res19CheckBox->setChecked(false);
    res19LineEdit->clear();
    }

    if (!record.isNull("RESERVED_20") )
    {
    QString reserved_20 = record.value("RESERVED_20").toString();
    res20CheckBox->setChecked(true);
    res20LineEdit->setText(reserved_20);
    }
    else
    {
    res20CheckBox->setChecked(false);
    res20LineEdit->clear();
    }

current_record = QSqlRecord(record);
current_index = QModelIndex(index);
}

void EditWidget::save_clicked()
{
QSqlRecord record = read_record(current_record);
bool fast_update = fastCommitCheckBox->isChecked();
emit record_to_commit( record, current_index, is_new, fast_update );
close();
}

// TODO
// put in schema structures
QSqlRecord EditWidget::read_record(QSqlRecord record)
{

    if ( nominalCheckBox->isChecked() )
    {
    record.setValue( "DENOMINATION", QVariant ( nominalComboBox->currentText() ) );
    }
    else
    {
    record.setNull("DENOMINATION");
    }

    if ( yearCheckBox->isChecked() )
    {
    record.setValue( "OBJ_YEAR", QVariant( yearSpinBox->value() ) );
    }
    else
    {
    record.setNull("OBJ_YEAR");
    }

    if ( marksCheckBox->isChecked() )
    {
    record.setValue("MARKS", QVariant( marksComboBox->currentText() ) );
    }
    else
    {
    record.setNull("MARKS");
    }

    if ( rarCheckBox->isChecked() )
    {
    record.setValue("RATING", QVariant( rarSpinBox->value() ) );
    }
    else
    {
    record.setNull("RATING");
    }

    if ( stateCheckBox->isChecked() )
    {
    record.setValue( "STATE", stateComboBox->currentText() );
    }
    else
    {
    record.setNull("STATE");
    }

    if ( epochCheckBox->isChecked() )
    {
    record.setValue( "EPOCH", QVariant( epochComboBox->currentText() ) );
    }
    else
    {
    record.setNull("EPOCH");
    }

    if ( edgeCheckBox->isChecked() )
    {
    record.setValue("EDGE", QVariant( edgeLineEdit->text() ) );
    }
    else
    {
    record.setNull("EDGE");
    }

    if ( materialCheckBox->isChecked() )
    {
    record.setValue( "MATERIAL", QVariant( materialComboBox->currentText() ) );
    }
    else
    {
    record.setNull("MATERIAL");
    }

    if ( gradedEditCheckBox->isChecked() )
    {
    record.setValue("GRADED", QVariant( QString("Y") ) );
    }
    else
    {
    record.setNull("GRADED");
    }

    if ( certEditCheckBox->isChecked() )
    {
    record.setValue("CERTIFIED", QVariant( QString("Y") ) );
    }
    else
    {
    record.setNull("CERTIFIED");
    }

    if ( conditionCheckBox->isChecked() )
    {
    record.setValue("CONDITION", QVariant( condComboBox->currentText() ) );
    }
    else
    {
    record.setNull("CONDITION");
    }

    if ( certAuthEditCheckBox->isChecked() )
    {
    record.setValue("CERT_AUTH",
                    QVariant( certAuthComboBox->currentText() ) );
    }
    else
    {
    record.setNull("CERT_AUTH");
    }

    if ( condNoteCheckBox->isChecked() )
    {
    record.setValue("COND_NOTE", QVariant(  condNoteLineEdit->text() ) );
    }
    else
    {
    record.setNull("COND_NOTE");
    }

    if ( weightCheckBox->isChecked() )
    {
    double weight = weightDoubleSpinBox->value();
    record.setValue("WEIGHT", QVariant( weight ) );
    record.setValue("WEIGHT_UNIT",
                    QVariant( weightUnitComboBox->currentText() ) );
    }
    else
    {
    record.setNull("WEIGHT");
    record.setNull("WEIGHT_UNIT");
    }

    if ( diamCheckBox->isChecked() )
    {
    double diam = diamDoubleSpinBox->value();
    record.setValue("DIAM", QVariant( diam ) );
    record.setValue("DIAM_UNIT", QVariant( diamUnitComboBox->currentText() ) );
    }
    else
    {
    record.setNull("DIAM");
    record.setNull("DIAM_UNIT");
    }

    if ( axis1CheckBox->isChecked() )
    {
    record.setValue("AXIS1", QVariant( axis1ComboBox->currentText() ) );
    }
    else
    {
    record.setNull("AXIS1");
    }

    if ( axis2CheckBox->isChecked() )
    {
    record.setValue("AXIS2", QVariant( axis2LineEdit->text() ) );
    }
    else
    {
    record.setNull("AXIS2");
    }

    if ( shapeCheckBox->isChecked() )
    {
    record.setValue("SHAPE", QVariant( shapeComboBox->currentText() ) );
    }
    else
    {
    record.setNull("SHAPE");
    }

    if ( thickCheckBox->isChecked() )
    {
    double thick = thickDoubleSpinBox->value();
    record.setValue("THICKNESS", QVariant( thick ) );
    record.setValue("THICKNESS_UNIT",
                     QVariant( thickUnitComboBox->currentText() ) );
    }
    else
    {
    record.setNull("THICKNESS");
    record.setNull("THICKNESS_UNIT");
    }

    if ( edgeIDCheckBox->isChecked() )
    {
    record.setValue("EDGEID", QVariant( edgeIDComboBox->currentText() ) );
    }
    else
    {
    record.setNull("EDGEID");
    }

    if ( typeCheckBox->isChecked() )
    {
    record.setValue("OBJ_TYPE", QVariant( typeComboBox->currentText() ) );
    }
    else
    {
    record.setNull("OBJ_TYPE");
    }

    if ( undertypeCheckBox->isChecked() )
    {
    record.setValue("OBJ_UNDERTYPE", QVariant( undertypeComboBox->currentText() ) );
    }
    else
    {
    record.setNull("OBJ_UNDERTYPE");
    }

    if ( edgeLegendCheckBox->isChecked() )
    {
    record.setValue("EDGE_LEGEND", QVariant( edgeLegendLineEdit->text() ) );
    }
    else
    {
    record.setNull("EDGE_LEGEND");
    }

    if ( artistObvCheckBox->isChecked() )
    {
    record.setValue("OBVERSEARTIST", QVariant( artistObvLineEdit->text() ) );
    }
    else
    {
    record.setNull("OBVERSEARTIST");
    }

    if ( artistRevCheckBox->isChecked() )
    {
    record.setValue("REVERSEARTIST", QVariant( artistRevLineEdit->text() ) );
    }
    else
    {
    record.setNull("REVERSEARTIST");
    }

    if ( engraverObvCheckBox->isChecked() )
    {
    record.setValue("OBVERSEENGRAVER", QVariant( engraverObvLineEdit->text() ) );
    }
    else
    {
    record.setNull("OBVERSEENGRAVER");
    }

    if ( engraverRevCheckBox->isChecked() )
    {
    record.setValue("REVERSEENGRAVER", QVariant( engraverRevLineEdit->text() ) );
    }
    else
    {
    record.setNull("REVERSEENGRAVER");
    }

    if ( issuerCheckBox->isChecked() )
    {
    record.setValue("ISSUER", QVariant( issuerLineEdit->text() ) );
    }
    else
    {
    record.setNull("ISSUER");
    }

    if ( authCheckBox->isChecked() )
    {
    record.setValue("AUTHORITY", QVariant( authLineEdit->text() ) );
    }
    else
    {
    record.setNull("AUTHORITY");
    }

    if ( mintCheckBox->isChecked() )
    {
    record.setValue("MINT", QVariant( mintComboBox->currentText() ) );
    }
    else
    {
    record.setNull("MINT");
    }

    if ( mintageNoteCheckBox->isChecked() )
    {
    record.setValue("MINTAGE_NOTE", QVariant( mintageNoteLineEdit->text() ) );
    }
    else
    {
    record.setNull("MINTAGE_NOTE");
    }

    if ( mintageCheckBox->isChecked() )
    {
    record.setValue("MINTAGE", QVariant( mintageSpinBox->value() ) );
    }
    else
    {
    record.setNull("MINTAGE");
    }

    if ( obvSymbolCheckBox->isChecked() )
    {
    record.setValue("OBVERSESYMBOL", QVariant( obvSymbolComboBox->currentText() ) );
    }
    else
    {
    record.setNull("OBVERSESYMBOL");
    }

    if ( obvDieIDCheckBox->isChecked() )
    {
    record.setValue("OBVERSEDIEID", QVariant( obvDieIDComboBox->currentText() ) );
    }
    else
    {
    record.setNull("OBVERSEDIEID");        
    }

    if ( revDieIDCheckBox->isChecked() )
    {
    record.setValue("REVERSEDIEID", QVariant( revDieIDComboBox->currentText() ) );
    }
    else
    {
    record.setNull("REVERSEDIEID");
    }
    
    if ( obvTypeCheckBox->isChecked() )
    {
    record.setValue("OBVERSETYPE", QVariant( obvTypeComboBox->currentText() ) );
    }
    else
    {
    record.setNull("OBVERSETYPE");
    }

    if ( obvLegend1CheckBox->isChecked() )
    {
    record.setValue("OBVERSELEGEND1", QVariant( obvLegend1LineEdit->text() ) );
    }
    else
    {
    record.setNull("OBVERSELEGEND1");
    }

    if ( obvLegend2CheckBox->isChecked() )
    {
    record.setValue("OBVERSELEGEND2", QVariant( obvLegend2LineEdit->text() ) );
    }
    else
    {
    record.setNull("OBVERSELEGEND2");
    }

    if ( obvMarkCheckBox->isChecked() )
    {
    record.setValue("OBVERSECOUNTERMARK", QVariant( obvMarkLineEdit->text() ) );
    }
    else
    {
    record.setNull("OBVERSECOUNTERMARK");
    }

    if ( revSymbolCheckBox->isChecked() )
    {
    record.setValue("REVERSESYMBOL", QVariant( revSymbolComboBox->currentText() ) );
    }
    else
    {
    record.setNull("REVERSESYMBOL");
    }

    if ( revTypeCheckBox->isChecked() )
    {
    record.setValue("REVERSETYPE", QVariant( revTypeComboBox->currentText() ) );
    }
    else
    {
    record.setNull("REVERSETYPE");
    }

    if ( revLegend1CheckBox->isChecked() )
    {
    record.setValue("REVERSELEGEND1", QVariant( revLegend1LineEdit->text() ) );
    }
    else
    {
    record.setNull("REVERSELEGEND1");
    }

    if ( revLegend2CheckBox->isChecked() )
    {
    record.setValue("REVERSELEGEND2", QVariant( revLegend2LineEdit->text() ) );
    }
    else
    {
    record.setNull("REVERSELEGEND2");
    }

    if ( revMarkCheckBox->isChecked() )
    {
    record.setValue("REVERSECOUNTERMARK", QVariant( revMarkLineEdit->text() ) );
    }
    else
    {
    record.setNull("REVERSECOUNTERMARK");
    }

    if ( fromCheckBox->isChecked() )
    {
    record.setValue("FROMDATE", QVariant( fromLineEdit->text() ) );
    }
    else
    {
    record.setNull("FROMDATE");
    }

    if ( toCheckBox->isChecked() )
    {
    record.setValue("TODATE", QVariant( toLineEdit->text() ) );
    }
    else
    {
    record.setNull("TODATE");
    }

    if ( dateObjCheckBox->isChecked() )
    {
    record.setValue("DATEONOBJECT", QVariant( dateObjLineEdit->text() ) );
    }
    else
    {
    record.setNull("DATEONOBJECT");
    }

    if ( eraObjCheckBox->isChecked() )
    {
    record.setValue("ERAONOBJECT", QVariant( eraObjLineEdit->text() ) );
    }
    else
    {
    record.setNull("ERAONOBJECT");
    }

    if ( rulerObjCheckBox->isChecked() )
    {
    record.setValue("RULERONOBJECT", QVariant( rulerObjLineEdit->text() ) );
    }
    else
    {
    record.setNull("RULERONOBJECT");
    }

    if ( regionCheckBox->isChecked() )
    {
    record.setValue("REGION", QVariant( regionLineEdit->text() ) );
    }
    else
    {
    record.setNull("REGION");
    }

    if ( fsCheckBox->isChecked() )
    {
    record.setValue("FINDSPOT", QVariant( fsLineEdit->text() ) );
    }
    else
    {
    record.setNull("FINDSPOT");
    }

    if ( coordCheckBox->isChecked() )
    {
    record.setValue("COORDINATES", QVariant( coordLineEdit->text() ) );
    }
    else
    {
    record.setNull("COORDINATES");
    }

    if ( latitudeCheckBox->isChecked() )
    {
    double latitude = latitudeSpinBox->value();
    record.setValue("LATITUDE", QVariant(latitude) );
    }
    else
    {
    record.setNull("LATITUDE");
    }

    if ( longitudeCheckBox->isChecked() )
    {
    double longitude = longitudeSpinBox->value();
    record.setValue("LONGITUDE", QVariant(longitude) );
    }
    else
    {
    record.setNull("LONGITUDE");
    }

    if ( statusCheckBox->isChecked() )
    {
    record.setValue("STATUS", QVariant( statusComboBox->currentText() ) );
    }
    else
    {
    record.setNull("STATUS");
    }

    if ( aqDateCheckBox->isChecked() )
    {
    record.setValue("AQ_DATE", QVariant( aqDateLineEdit->text() ) );
    }
    else
    {
    record.setNull("AQ_DATE");
    }

    if ( locCheckBox->isChecked() )
    {
    record.setValue("LOCATION", QVariant( locComboBox->currentText() ) );
    }
    else
    {
    record.setNull("LOCATION");
    }

    if ( sellerCheckBox->isChecked() )
    {
    record.setValue("SELLER", QVariant( sellerComboBox->currentText() ) );
    }
    else
    {
    record.setNull("SELLER");
    }

    if ( priceCheckBox->isChecked() )
    {
    double price = priceDoubleSpinBox->value();
    record.setValue("AQ_PRICE", QVariant( price ) );
    record.setValue("AQ_CURR",
                    QVariant( priceCurrComboBox->currentText() ) );
    }
    else
    {
    record.setNull("AQ_PRICE");
    record.setNull("AQ_CURR");
    }

    if ( estPriceCheckBox->isChecked() )
    {
    double estim = estPriceDoubleSpinBox->value();
    record.setValue("ACT_VALUE", QVariant( estim ) );
    record.setValue("ACT_CURR",
                    QVariant( estPriceCurrComboBox->currentText() ) );
    }
    else
    {
    record.setNull("ACT_VALUE");
    record.setNull("ACT_CURR");
    }

    if ( soldDateCheckBox->isChecked() )
    {
    QDate d = dateEdit->date();
    QString d_string = d.toString("yyyyMMdd");
    bool ok = false;
    int d_int = d_string.toInt( &ok, 10 );
        if (ok)
        {
        record.setValue("SOLD_DATE",  d_int);
        }
        else
        {
        record.setNull("SOLD_DATE");
        }
    }
    else
    {
    record.setNull("SOLD_DATE");
    }

    if ( custCheckBox->isChecked() )
    {
    record.setValue("CUSTOMER", QVariant( custComboBox->currentText() ) );
    }
    else
    {
    record.setNull("CUSTOMER");
    }

    if ( titelCheckBox->isChecked() )
    {
    record.setValue("TITLE", QVariant( titelLineEdit->text() ) );
    }
    else
    {
    record.setNull("TITLE");
    }

    if ( uniqIDCheckBox->isChecked() )
    {
    record.setValue("UNIQUEID", QVariant( uniqIDLineEdit->text() ) );
    }
    else
    {
    record.setNull("UNIQUEID");
    }

    if ( collCheckBox->isChecked() )
    {
    record.setValue("COLLECTION", QVariant( collLineEdit->text() ) );
    }
    else
    {
    record.setNull("COLLECTION");
    }

    if ( collIDCheckBox->isChecked() )
    {
    record.setValue("COLLECTIONID", QVariant( collIDLineEdit->text() ) );
    }
    else
    {
    record.setNull("COLLECTIONID");
    }

    if ( projIDCheckBox->isChecked() )
    {
    record.setValue("PROJECTID", QVariant( projIDLineEdit->text() ) );
    }
    else
    {
    record.setNull("PROJECTID");
    }

    if ( hoardCheckBox->isChecked() )
    {
    record.setValue("HOARD", QVariant( hoardLineEdit->text() ) );
    }
    else
    {
    record.setNull("HOARD");
    }

    if ( catIDCheckBox->isChecked() )
    {
    record.setValue("CATALOGID", QVariant( catIDLineEdit->text() ) );
    }
    else
    {
    record.setNull("CATALOGID");
    }

    if ( historyCheckBox->isChecked() )
    {
    record.setValue("HISTORY_NOTE", QVariant( historyLineEdit->text() ) );
    }
    else
    {
    record.setNull("HISTORY_NOTE");
    }

    if ( recCreatedCheckBox->isChecked() )
    {
    record.setValue("RECORDDATE", QVariant( recCreatedLineEdit->text() ) );
    }
    else
    {
    record.setNull("RECORDDATE");
    }

    if ( recTypeCheckBox->isChecked() )
    {
    record.setValue("RECORDTYPE", QVariant( recTypeComboBox->currentText() ) );
    }
    else
    {
    record.setNull("RECORDTYPE");
    }

    if ( res1CheckBox->isChecked() )
    {
    record.setValue("RESERVED_1", QVariant( res1LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_1");
    }

    if ( res2CheckBox->isChecked() )
    {
    record.setValue("RESERVED_2", QVariant( res2LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_2");
    }

    if ( res3CheckBox->isChecked() )
    {
    record.setValue("RESERVED_3", QVariant( res3LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_3");
    }

    if ( res4CheckBox->isChecked() )
    {
    record.setValue("RESERVED_4", QVariant( res4LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_4");
    }

    if ( res5CheckBox->isChecked() )
    {
    record.setValue("RESERVED_5", QVariant( res5LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_5");
    }

    if ( res6CheckBox->isChecked() )
    {
    record.setValue("RESERVED_6", QVariant( res6LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_6");
    }

    if ( res7CheckBox->isChecked() )
    {
    record.setValue("RESERVED_7", QVariant( res7LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_7");
    }

    if ( res8CheckBox->isChecked() )
    {
    record.setValue("RESERVED_8", QVariant( res8LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_8");
    }

    if ( res9CheckBox->isChecked() )
    {
    record.setValue("RESERVED_9", QVariant( res9LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_9");
    }

    if ( res10CheckBox->isChecked() )
    {
    record.setValue("RESERVED_10", QVariant( res10LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_10");
    }

    if ( res11CheckBox->isChecked() )
    {
    record.setValue("RESERVED_11", QVariant( res11LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_11");
    }

    if ( res12CheckBox->isChecked() )
    {
    record.setValue("RESERVED_12", QVariant( res12LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_12");
    }

    if ( res13CheckBox->isChecked() )
    {
    record.setValue("RESERVED_13", QVariant( res13SpinBox->value() ) );
    }
    else
    {
    record.setNull("RESERVED_13");
    }

    if ( res14CheckBox->isChecked() )
    {
    record.setValue("RESERVED_14", QVariant( res14SpinBox->value() ) );
    }
    else
    {
    record.setNull("RESERVED_14");
    }

    if ( res15CheckBox->isChecked() )
    {
    record.setValue("RESERVED_15", QVariant( res15SpinBox->value() ) );
    }
    else
    {
    record.setNull("RESERVED_15");
    }

    if ( res16CheckBox->isChecked() )
    {
    record.setValue("RESERVED_16", QVariant( res16SpinBox->value() ) );
    }
    else
    {
    record.setNull("RESERVED_16");
    }

    if ( res17CheckBox->isChecked() )
    {
    record.setValue("RESERVED_17", QVariant( res17DoubleSpinBox->value() ) );
    }
    else
    {
    record.setNull("RESERVED_17");
    }

    if ( res18CheckBox->isChecked() )
    {
    record.setValue("RESERVED_18", QVariant( res18DoubleSpinBox->value() ) );
    }
    else
    {
    record.setNull("RESERVED_18");
    }

    if ( res19CheckBox->isChecked() )
    {
    QString long_str = res19LineEdit->text();
    bool conv_ok = false;
    qint64 reserved_19 = long_str.toLongLong(&conv_ok);
        if ( true == conv_ok )
        {
        record.setValue("RESERVED_19", QVariant(reserved_19) );
        }
        else
        {
        record.setNull("RESERVED_19");
        }
    }
    else
    {
    record.setNull("RESERVED_19");
    }

    if ( res20CheckBox->isChecked() )
    {
    record.setValue("RESERVED_20", QVariant( res20LineEdit->text() ) );
    }
    else
    {
    record.setNull("RESERVED_20");
    }

return record;
}

// TODO
// put in schema structures
void EditWidget::new_record(QString opened_album)
{
    if ( true == opened_album.isEmpty() )
    {
    return;
    }
QSqlRecord record = QSqlRecord();
qint64 id = Q_INT64_C(-1);

    {
    QSqlDatabase db = QSqlDatabase::database("collection");
       if ( !db.isOpen() )
       {
       return;
       }
    QString s = QLatin1String("select gen_id(");
    s.append(opened_album);
    s.append(QLatin1String("_a_gen,1) from rdb$database"));
    QSqlQuery query(s, db);
        while (query.next())
        {
        id = query.value(0).toLongLong();
        }
    query.clear();
    }

fastCommitCheckBox->setEnabled(false);

record.insert( 0, QSqlField("ID", QVariant::LongLong) );
record.setValue("ID", id);
is_new = true;

record.insert(  1, QSqlField("DENOMINATION", QVariant::String) );
record.insert(  2, QSqlField("OBJ_YEAR", QVariant::Int) );
record.insert(  3, QSqlField("MARKS", QVariant::String) );
record.insert(  4, QSqlField("RATING", QVariant::Int) );
record.insert(  5, QSqlField("STATE", QVariant::String) );
record.insert(  6, QSqlField("EPOCH", QVariant::String) );
record.insert(  7, QSqlField("EDGE", QVariant::String) );
record.insert(  8, QSqlField("MATERIAL", QVariant::String) );
record.insert(  9, QSqlField("GRADED", QVariant::String) );
record.insert( 10, QSqlField("CONDITION", QVariant::String) );
record.insert( 11, QSqlField("CERTIFIED", QVariant::String) );
record.insert( 12, QSqlField("CERT_AUTH", QVariant::String) );
record.insert( 13, QSqlField("COND_NOTE", QVariant::String) );
record.insert( 14, QSqlField("WEIGHT", QVariant::Double) );
record.insert( 15, QSqlField("WEIGHT_UNIT", QVariant::String) );
record.insert( 16, QSqlField("DIAM", QVariant::Double) );
record.insert( 17, QSqlField("DIAM_UNIT", QVariant::String) );
record.insert( 18, QSqlField("AXIS1", QVariant::String) );
record.insert( 19, QSqlField("AXIS2", QVariant::String) );
record.insert( 20, QSqlField("SHAPE", QVariant::String) );
record.insert( 21, QSqlField("THICKNESS", QVariant::Double) );
record.insert( 22, QSqlField("THICKNESS_UNIT", QVariant::String) );
record.insert( 23, QSqlField("EDGEID", QVariant::String) );
record.insert( 24, QSqlField("COMPLITENESS", QVariant::String) );
record.insert( 25, QSqlField("OBJ_TYPE", QVariant::String) );
record.insert( 26, QSqlField("OBJ_UNDERTYPE", QVariant::String) );
record.insert( 27, QSqlField("EDGE_LEGEND", QVariant::String) );
record.insert( 28, QSqlField("OBVERSEARTIST", QVariant::String) );
record.insert( 29, QSqlField("REVERSEARTIST", QVariant::String) );
record.insert( 30, QSqlField("OBVERSEENGRAVER", QVariant::String) );
record.insert( 31, QSqlField("REVERSEENGRAVER", QVariant::String) );
record.insert( 32, QSqlField("ISSUER", QVariant::String) );
record.insert( 33, QSqlField("AUTHORITY", QVariant::String) );
record.insert( 34, QSqlField("MINT", QVariant::String) );
record.insert( 35, QSqlField("MINTAGE_NOTE", QVariant::String) );
record.insert( 36, QSqlField("MINTAGE", QVariant::Int) );
record.insert( 37, QSqlField("OBVERSESYMBOL", QVariant::String) );
record.insert( 38, QSqlField("OBVERSETYPE", QVariant::String) );
record.insert( 39, QSqlField("OBVERSELEGEND1", QVariant::String) );
record.insert( 40, QSqlField("OBVERSELEGEND2", QVariant::String) );
record.insert( 41, QSqlField("OBVERSEDIEID", QVariant::String) );
record.insert( 42, QSqlField("OBVERSECOUNTERMARK", QVariant::String) );
record.insert( 43, QSqlField("REVERSESYMBOL", QVariant::String) );
record.insert( 44, QSqlField("REVERSETYPE", QVariant::String) );
record.insert( 45, QSqlField("REVERSELEGEND1", QVariant::String) );
record.insert( 46, QSqlField("REVERSELEGEND2", QVariant::String) );
record.insert( 47, QSqlField("REVERSEDIEID", QVariant::String) );
record.insert( 48, QSqlField("REVERSECOUNTERMARK", QVariant::String) );
record.insert( 49, QSqlField("FROMDATE", QVariant::String) );
record.insert( 50, QSqlField("TODATE", QVariant::String) );
record.insert( 51, QSqlField("DATEONOBJECT", QVariant::String) );
record.insert( 52, QSqlField("ERAONOBJECT", QVariant::String) );
record.insert( 53, QSqlField("RULERONOBJECT", QVariant::String) );
record.insert( 54, QSqlField("REGION", QVariant::String) );
record.insert( 55, QSqlField("FINDSPOT", QVariant::String) );
record.insert( 56, QSqlField("COORDINATES", QVariant::String) );
record.insert( 57, QSqlField("LATITUDE", QVariant::Double) );
record.insert( 58, QSqlField("LONGITUDE", QVariant::Double) );
record.insert( 59, QSqlField("STATUS", QVariant::String) );
record.insert( 60, QSqlField("AQ_DATE", QVariant::String) );
record.insert( 61, QSqlField("LOCATION", QVariant::String) );
record.insert( 62, QSqlField("SELLER", QVariant::String) );
record.insert( 63, QSqlField("AQ_PRICE", QVariant::Double) );
record.insert( 64, QSqlField("AQ_CURR", QVariant::String) );
record.insert( 65, QSqlField("ACT_VALUE", QVariant::Double) );
record.insert( 66, QSqlField("ACT_CURR", QVariant::String) );
record.insert( 67, QSqlField("SOLD_DATE", QVariant::Int) );
record.insert( 68, QSqlField("CUSTOMER", QVariant::String) );
record.insert( 69, QSqlField("OWNER", QVariant::String) );
record.insert( 70, QSqlField("TITLE", QVariant::String) );
record.insert( 71, QSqlField("UNIQUEID", QVariant::String) );
record.insert( 72, QSqlField("COLLECTION", QVariant::String) );
record.insert( 73, QSqlField("COLLECTIONID", QVariant::String) );
record.insert( 74, QSqlField("PROJECTID", QVariant::String) );
record.insert( 75, QSqlField("HOARD", QVariant::String) );
record.insert( 76, QSqlField("CATALOGID", QVariant::String) );
record.insert( 77, QSqlField("HISTORY_NOTE", QVariant::String) );
record.insert( 78, QSqlField("RECORDDATE", QVariant::String) );
record.insert( 79, QSqlField("RECORDTYPE", QVariant::String) );
record.insert( 80, QSqlField("RESERVED_1", QVariant::String) );
record.insert( 81, QSqlField("RESERVED_2", QVariant::String) );
record.insert( 82, QSqlField("RESERVED_3", QVariant::String) );
record.insert( 83, QSqlField("RESERVED_4", QVariant::String) );
record.insert( 84, QSqlField("RESERVED_5", QVariant::String) );
record.insert( 85, QSqlField("RESERVED_6", QVariant::String) );
record.insert( 86, QSqlField("RESERVED_7", QVariant::String) );
record.insert( 87, QSqlField("RESERVED_8", QVariant::String) );
record.insert( 88, QSqlField("RESERVED_9", QVariant::String) );
record.insert( 89, QSqlField("RESERVED_10", QVariant::String) );
record.insert( 90, QSqlField("RESERVED_11", QVariant::String) );
record.insert( 91, QSqlField("RESERVED_12", QVariant::String) );
record.insert( 92, QSqlField("RESERVED_13", QVariant::Int) );
record.insert( 93, QSqlField("RESERVED_14", QVariant::Int) );
record.insert( 94, QSqlField("RESERVED_15", QVariant::Int) );
record.insert( 95, QSqlField("RESERVED_16", QVariant::Int) );
record.insert( 96, QSqlField("RESERVED_17", QVariant::Double) );
record.insert( 97, QSqlField("RESERVED_18", QVariant::Double) );
record.insert( 98, QSqlField("RESERVED_19", QVariant::LongLong) );
record.insert( 99, QSqlField("RESERVED_20", QVariant::String) );

current_record = QSqlRecord(record);

QDate d = QDate::currentDate();
dateEdit->setDate(d);

}

void EditWidget::writeSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("EditWidget") );
settings.setValue( QLatin1String("size"), QVariant( this->size() ) );
settings.setValue( QLatin1String("pos"), QVariant( this->pos() ) );
settings.endGroup();
}

void EditWidget::readSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("EditWidget") );
resize( settings.value( QLatin1String("size"), QSize( 800, 550 ) ).toSize() );
move( settings.value( QLatin1String("pos"), QPoint( 0, 0 ) ).toPoint() );
settings.endGroup();
}

void EditWidget::closeEvent( QCloseEvent * e )
{
writeSettings();
e->accept();
}

void EditWidget::show_chars()
{
CharsWidget * w = new CharsWidget();
w->set_model();
w->show();
}
